package com.ce.server.pojo;

import cn.afterturn.easypoi.excel.annotation.Excel;
import cn.afterturn.easypoi.excel.annotation.ExcelEntity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;

import java.io.Serializable;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

import com.ce.server.common.CustomAuthorityDeserializer;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

/**
 * <p>
 *
 * </p>
 *
 * @author 郑爽
 * @since 2021-11-17
 */
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName("t_student")
@ApiModel(value = "Student对象", description = "")
public class Student implements Serializable, UserDetails {

  private static final long serialVersionUID = 1L;

  @ApiModelProperty(value = "学生id")
  @TableId(value = "id", type = IdType.AUTO)
  private Integer id;

  @ApiModelProperty(value = "学生学号")
  @Excel(name = "学生学号")
  private String sno;

  @ApiModelProperty(value = "学生姓名")
  @Excel(name = "学生姓名")
  private String sname;

  @ApiModelProperty(value = "登录账号密码")
  private String pass;

  @ApiModelProperty(value = "0代表男 1 代表女")
  @Excel(name = "性别")
  private Integer gender;

  @ApiModelProperty(value = "年龄")
  @Excel(name = "年龄")
  private Integer age;

  @ApiModelProperty(value = "级别")
  @Excel(name = "级别")
  private String grade;

  @ApiModelProperty(value = "专业")
  @Excel(name = "专业", width = 18)
  private String major;

  @ApiModelProperty(value = "健康码状态，绿码或者非绿码")
  @Excel(name = "健康码状态", width = 15)
  private String jkm_status;

  @ApiModelProperty(value = "籍贯")
  @Excel(name = "籍贯")
  private String nativePlace;

  @ApiModelProperty(value = "联系地址")
  @Excel(name = "联系地址")
  private String address;

  @ApiModelProperty(value = "联系电话")
  @Excel(name = "联系电话")
  private String telephone;

  @ApiModelProperty(value = "身份证号码")
  @Excel(name = "身份证号码")
  private String idCard;

  @ApiModelProperty(value = "行政班级")
  @Excel(name = "行政班级")
  private String classInfo;

  @ApiModelProperty(value = "出生日期")
  @JsonFormat(pattern = "yyyy-MM-dd", timezone = "Asia/Shanghai")
  @Excel(name = "出生日期", format = "yyyy-MM-dd", width = 20)
  private LocalDate birthday;

  @ApiModelProperty(value = "政治面貌id")
  private Integer politicId;

  @ApiModelProperty(value = "政治面貌")
  @TableField(exist = false)
  @ExcelEntity(name = "政治面貌")
  private PoliticsStatus politicsStatus;

  @ApiModelProperty(value = "民族id")
  private Integer nationId;

  @ApiModelProperty(value = "民族")
  @TableField(exist = false)
  @ExcelEntity(name = "民族")
  private Nation nation;

  @ApiModelProperty(value = "角色列表")
  @TableField(exist = false)
  private List<Role> roles;

  @ApiModelProperty(value = "用户头像")
  private String userface;


  @Override
  @JsonDeserialize(using = CustomAuthorityDeserializer.class)
  public Collection<? extends GrantedAuthority> getAuthorities() {
    List<SimpleGrantedAuthority> authorities = roles.stream()
            .map(role -> new SimpleGrantedAuthority(role.getName()))
            .collect(Collectors.toList());
    return authorities;
  }

  @Override
  public String getPassword() {
    return pass;
  }

  @Override
  public String getUsername() {
    return sno;
  }

  @Override
  public boolean isAccountNonExpired() {
    return true;
  }

  @Override
  public boolean isAccountNonLocked() {
    return true;
  }

  @Override
  public boolean isCredentialsNonExpired() {
    return true;
  }

  @Override
  public boolean isEnabled() {
    return true;
  }
}
